#define _CRT_SECURE_NO_WARNINGS 1
#include<vector>
#include<map>
using namespace std;
class Solution {
public:
    vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
        vector<pair<int, int>> v;
        vector<int> ret;
        multimap<int, int> m;
        for (int i = 0; i < mat.size(); i++)
        {
            int count = 0, j = 0;
            while (j < mat[i].size() && mat[i][j])
            {
                count++;
                j++;
            }
            v.push_back(make_pair(count, i));
        }
        for (auto& kv : v)
        {
            m.insert(make_pair(kv.first, kv.second));
        }
        for (auto& kv : m)
        {
            ret.push_back(kv.second);
            k--;
            if (k == 0)
            {
                break;
            }
        }
        return ret;
    }
};